package com.itany.nmms.service.impl;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.ProductTypeMapper;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.entity.ProductTypeExample;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date 2025/9/29
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {

    private ProductTypeMapper typeMapper;

    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException {
        if(ParameterUtil.isNull(name)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andNameEqualTo(name);
        List<ProductType> types = typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new ProductTypeExistException("该商品类型已存在");
        }

        ProductType type = new ProductType();
        type.setName(name);
        type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        typeMapper.insertSelective(type);
    }

    @Override
    public PageInfo<ProductType> findPage(String pageNo, String pageSize) throws RequestParameterErrorException {

        return null;
    }
}
